package com.iotproject.studyservice.controller;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyGrade;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.service.SelectWordService;
import com.iotproject.studyservice.service.StudyGradeService;
import com.iotproject.studyservice.service.StudyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/studyservice/select-word")
public class SelectWordController {

    @Autowired
    StudyGradeService studyGradeService;

    @Autowired
    StudyWordService studyWordService;

    @Autowired
    SelectWordService selectWordService;

    //获取 背单词 界面列表
    @GetMapping("getSelectWordList")
    public R getSelectWordList(HttpServletRequest httpServletRequest) {
        return selectWordService.getSelectWordList(httpServletRequest);
    }

    //根据等级找出等级名称
    @GetMapping("queryGradeNameById/{id}")
    public R queryGradeNameByGrade(@PathVariable("id") String id) {
        StudyGrade studyGrade = studyGradeService.getById(id);
        return R.ok().data("gradeName", studyGrade.getName());
    }
    //根据等级随机查询某个单词(简化方法，直接在 study-word 表里随机查找)
    @GetMapping("queryStudyWordByGrade/{grade}")
    public R queryStudyWordByGrade(@PathVariable("grade") String grade, HttpServletRequest httpServletRequest) {
        StudyWord studyWord = selectWordService.queryStudyWordByGrade(grade, httpServletRequest);
        if (studyWord == null) {
            StudyWord studyWordRandom = selectWordService.queryStudyWordByGradeRandom(grade);
            return R.ok().data("word", studyWordRandom);
        }
        return R.ok().data("word", studyWord);
    }
    //根据等级找出相关比例
    @GetMapping("progress/{grade}")
    public R progress(@PathVariable("grade") String grade, HttpServletRequest httpServletRequest) {
        Map<String, Double> progress = studyWordService.progress(grade, httpServletRequest);
        Double studyPercent = progress.get("studyPercent");
        Double rememberPercent = progress.get("rememberPercent");
        Double forgetPercent = progress.get("forgetPercent");
        return R.ok().data("studyPercent", studyPercent).data("rememberPercent", rememberPercent).data("forgetPercent", forgetPercent);
    }



}
